package com.shawn.spring.boot.mybatisplus.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import java.util.Calendar;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * @author shawn
 * @since 2020/8/30
 */
@Component
@Slf4j
public class MybatisPlusCommonFieldHandler implements MetaObjectHandler {

  @Override
  public void insertFill(MetaObject metaObject) {
    log.info("start insert fill ....");
    Date time = Calendar.getInstance().getTime();
    this.setFieldValByName("createTime", time, metaObject);
    this.setFieldValByName("updateTime", time, metaObject);
  }

  @Override
  public void updateFill(MetaObject metaObject) {
    log.info("start update fill ....");
    this.setFieldValByName("updateTime", Calendar.getInstance().getTime(), metaObject);
  }
}
